package com.dyh.quickdemo.demos.lbdand;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.function.Function;
import java.util.function.Predicate;

/*
@Author=Joey Wheeler
@Date=2020/12/8 13:32
*/
@RestController
@RequestMapping("/TestTaobaoClientController")
public class TestTaobaoClientController {

    TaobaoClient taobaoClient = new TaobaoClient();


    Function<BaseTaobaoReq,BaseTaobaoRsp> fallback = e -> {
        BaseTaobaoRsp rsp = new BaseTaobaoRsp();
        rsp.setMsg("授权过期");
        rsp.setSuccess(false);
        return rsp;
    };

    Predicate<BaseTaobaoRsp> fallbackTrigger = e-> !StringUtils.isEmpty(e.getMsg())&& e.getMsg().contains("授权过期");



    Function<BaseTaobaoReq,BaseTaobaoRsp> function =
            FcBreaker.with("helloTaobao",
                    taobaoClient::exe,fallback,fallbackTrigger);

    @GetMapping("/send/{token}")
    public void send(@PathVariable(name = "token") String token){
        BaseTaobaoReq req = new BaseTaobaoReq();
        req.setToken(token);
        function.apply(req);

    }


}
